﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.Base.Global.ExternalGlobalSettings
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using MCM.Abstractions.FluentBuilder;
using MCM.Abstractions.Utils;
using MCM.Abstractions.Xml;
using MCM.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

#nullable enable
namespace MCM.Abstractions.Base.Global
{
  public sealed class ExternalGlobalSettings : FluentGlobalSettings
  {
    private static SettingsPropertyDefinition FromXml(
      IPropertyGroupDefinition group,
      PropertyBaseXmlModel xmlModel,
      char subGroupDelimiter)
    {
      if (true)
        ;
      SettingsPropertyDefinition propertyDefinition;
      switch (xmlModel)
      {
        case PropertyBoolXmlModel property1:
          propertyDefinition = new SettingsPropertyDefinition(SettingsUtils.GetPropertyDefinitionWrappers((object) property1), group, (IRef) new StorageRef<bool>(property1.Value), subGroupDelimiter);
          break;
        case PropertyDropdownXmlModel property2:
          propertyDefinition = new SettingsPropertyDefinition(SettingsUtils.GetPropertyDefinitionWrappers((object) property2), group, (IRef) new StorageRef<Dropdown<string>>(new Dropdown<string>((IEnumerable<string>) property2.Values, property2.SelectedIndex)), subGroupDelimiter);
          break;
        case PropertyFloatingIntegerXmlModel property3:
          propertyDefinition = new SettingsPropertyDefinition(SettingsUtils.GetPropertyDefinitionWrappers((object) property3), group, (IRef) new StorageRef<float>((float) property3.Value), subGroupDelimiter);
          break;
        case PropertyIntegerXmlModel property4:
          propertyDefinition = new SettingsPropertyDefinition(SettingsUtils.GetPropertyDefinitionWrappers((object) property4), group, (IRef) new StorageRef<int>((int) property4.Value), subGroupDelimiter);
          break;
        case PropertyTextXmlModel property5:
          propertyDefinition = new SettingsPropertyDefinition(SettingsUtils.GetPropertyDefinitionWrappers((object) property5), group, (IRef) new StorageRef<string>(property5.Value), subGroupDelimiter);
          break;
        default:
          if (true)
            ;
          // ISSUE: reference to a compiler-generated method
          \u003CPrivateImplementationDetails\u003E.ThrowInvalidOperationException();
          break;
      }
      if (true)
        ;
      return propertyDefinition;
    }

    public static ExternalGlobalSettings? CreateFromXmlFile(
      string filePath,
      PropertyChangedEventHandler? propertyChanged = null)
    {
      using (FileStream xmlStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
      {
        SettingsXmlModel settingsXmlModel = SerializationUtils.DeserializeXml<SettingsXmlModel>((Stream) xmlStream);
        if (settingsXmlModel == null)
          return (ExternalGlobalSettings) null;
        char subGroupDelimiter = settingsXmlModel.SubGroupDelimiter[0];
        IEnumerable<SettingsPropertyDefinition> settingsPropertyDefinitions = settingsXmlModel.Groups.SelectMany<PropertyGroupXmlModel, SettingsPropertyDefinition>((Func<PropertyGroupXmlModel, IEnumerable<SettingsPropertyDefinition>>) (g => g.Properties.Select<PropertyBaseXmlModel, SettingsPropertyDefinition>((Func<PropertyBaseXmlModel, SettingsPropertyDefinition>) (p => ExternalGlobalSettings.FromXml((IPropertyGroupDefinition) g, p, subGroupDelimiter))))).Concat<SettingsPropertyDefinition>(settingsXmlModel.Properties.Select<PropertyBaseXmlModel, SettingsPropertyDefinition>((Func<PropertyBaseXmlModel, SettingsPropertyDefinition>) (p => ExternalGlobalSettings.FromXml(SettingsPropertyGroupDefinition.DefaultGroup, p, subGroupDelimiter))));
        List<SettingsPropertyGroupDefinition> settingsPropertyGroups = SettingsUtils.GetSettingsPropertyGroups(subGroupDelimiter, (IEnumerable<ISettingsPropertyDefinition>) settingsPropertyDefinitions);
        return new ExternalGlobalSettings(settingsXmlModel.Id, settingsXmlModel.DisplayName, settingsXmlModel.FolderName, settingsXmlModel.SubFolder, settingsXmlModel.FormatType, settingsXmlModel.UIVersion, subGroupDelimiter, propertyChanged, (IEnumerable<SettingsPropertyGroupDefinition>) settingsPropertyGroups, (IEnumerable<ISettingsPresetBuilder>) new List<ISettingsPresetBuilder>(), filePath);
      }
    }

    public string FilePath { get; init; }

    private ExternalGlobalSettings(
      string id,
      string displayName,
      string folderName,
      string subFolder,
      string format,
      int uiVersion,
      char subGroupDelimiter,
      PropertyChangedEventHandler? onPropertyChanged,
      IEnumerable<SettingsPropertyGroupDefinition> settingPropertyGroups,
      IEnumerable<ISettingsPresetBuilder> presets,
      string filePath)
      : base(id, displayName, folderName, subFolder, format, uiVersion, subGroupDelimiter, onPropertyChanged, settingPropertyGroups, presets)
    {
      this.FilePath = filePath;
    }
  }
}
